1656B - Subtract Operation - CodeForces Solution


data structures math two pointers

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase
 
BUFSIZE = 8192
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()
 
	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()
 
	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def inpa(): return list(map(int, input().split()))
def outa(var): print(' '.join(map(str, var)))

def redirect_io():
    import pathlib, sys
    fname = pathlib.Path(__file__).parent/"input.txt"
    sys.stdin = open(fname, 'r')

def solve(n, k, arr):
    arr.sort()
    l, r = 0, 1
    while r < n:
        while (r < n and arr[r] - arr[l] < k):
            r += 1
        if r == n:
            break
        if arr[r] - arr[l] == k:
            return "YES"
        l += 1

    return "NO"

ans = []
for _ in range(int(input())):
    n, k = inpa()
    arr = inpa()
    ans.append(solve(n, k, arr))
print('\n'.join(ans))

C++ Code:

#include<bits/stdc++.h>
#include<string>
#include<vector>
#define mod 1000000007
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int t;
	cin >> t;
	while(t--)
	{
		int n, k, key;
		cin >> n >> k;
        int i = 0, s = 0;
		int a[n];
		for(i=0; i<n; i++)
		{
			cin >> a[i];
		}
		sort(a, a+n);
	    bool flag = false;
		for(i=0; i<n; i++)
		{
			key = a[i]+k;
			flag = binary_search(a+i+1, a+n, key);
			if(flag) break;
		}
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


Comments

Submit
0 Comments
More Questions

1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year